String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

(function (original) {
    jQuery.fn.clone = function () {
        var result = original.apply(this, arguments), my_textareas = this.find(
                'textarea').add(this.filter('textarea')), result_textareas = result
                .find('textarea').add(result.filter('textarea')), my_selects = this
                .find('select').add(this.filter('select')), result_selects = result
                .find('select').add(result.filter('select'));

        for (var i = 0, l = my_textareas.length; i < l; ++i)
            $(result_textareas[i]).val($(my_textareas[i]).val());
        for (var i = 0, l = my_selects.length; i < l; ++i)
            result_selects[i].selectedIndex = my_selects[i].selectedIndex;

        return result;
    };
})(jQuery.fn.clone);

(function ($) {
    $.fn.serializeJson = function () {
        var serializeObj = {};
        var array = this.serializeArray();
        var str = this.serialize();
        $(array).each(
                function () {
                    if (serializeObj[this.name]) {
                        if ($.isArray(serializeObj[this.name])) {
                            serializeObj[this.name].push(this.value);
                        } else {
                            serializeObj[this.name] = [
                                serializeObj[this.name], this.value];
                        }
                    } else {
                        serializeObj[this.name] = this.value;
                    }
                });
        return serializeObj;
    };
})(jQuery);

// 座机号码验证
jQuery.validator.addMethod("isTel", function (value, element) {
    // 可以匹配座机号码，形式如0511-12345678-1234，其中区号可以3位或4位或没有，直拨号码可以7位或8位，分机号可以为0至4位
    var tel = /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}(-\d{1,4})?$/;
    return this.optional(element) || (tel.test(value));
}, "格式");

// 手机号码验证
jQuery.validator.addMethod("isMobile", function (value, element) {
    var length = value.length;
    var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "格式不正确");

// 身份证号码验证
jQuery.validator.addMethod("isIdNo", function (value, element) {
    var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
    return this.optional(element) || (reg.test(value));
}, "格式不正确");

function selectBeforSubmit() {
    $("select[init='option']").change(function (e) {
        var text = $(this).find("option:selected").text();
        $(this).next().val(text);
    });
}


// 清空窗口
function clearinput(fm) {
    $(fm).find("input[type='text'],select,textarea").each(function () {
        $(this).val("");
    });
}

function json2form(pojo, targetformid) {
    for (var attr in pojo) {
        $("" + targetformid + " [name='" + attr + "']").attr("value",
                pojo[attr]);
    }
}

function delrow(e) {
    $(e).parent().parent().remove();
}

function addrow(e, clear) {
    var row = $(e).parent().parent();
    row.after("<tr>" + row.html() + "</tr>");
    if (clear) {
        row.next().find("input").val("");
    }
}

function checkall(id) {
    $("#" + id + " input[type='checkbox']").attr("checked", "true");
}

function notcheckall(id) {
    $("#" + id + " input[type='checkbox']").removeAttr("checked");
}

function getCheck(form, id, attrs) {
    var data = [];
    var attr = attrs.split(",");
    for (var i = 0; i < attr.length; i++) {
        $(form).find("[name='" + attr[i] + "']").remove();
    }
    $("#" + id + " input[type='checkbox']:checkbox:checked").each(
            function () {
                for (var i = 0; i < attr.length; i++) {
                    $(form).append(
                            "<input type='hidden' value='"
                            + $(this).attr(attr[i]) + "' name='"
                            + attr[i] + "'/>");
                    data.push($(this).attr(attr[i]));
                }
            });
    return data;
}

function getCheck2(id, attr) {
    var data = [];
    $("#" + id + " input[type='checkbox']:checkbox:checked").each(
            function () {
                data.push($(this).attr(attr));
            });
    return data;
}

function closetb() {
    parent.tb_remove();
}
